import { Component, OnInit, Input, OnDestroy } from '@angular/core';

import { Subscription } from 'rxjs';

import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from '../../shared/service/http.service';
import { MessageService } from '../../shared/service/message.service';

@Component({
  selector: 'app-basic-list',
  templateUrl: './basic-list.component.html',
  styleUrls: ['./basic-list.component.scss']
})
export class BasicListComponent implements OnInit, OnDestroy {

  // 组件数据
  @Input() tagData: any = {};

  // 声明订阅对象
  subscription: Subscription = null;

  // 筛选及页面数据参数
  params: any = {};

  // 当前页数，默认1
  pageNo = 1;

  // size
  pageSize: number;

  // 数量
  pageTotal: number;

  constructor(private appGlobalService: AppGlobalService, private message: MessageService, private http: HttpService) { }

  ngOnInit() {
    // 初始化参数
    this.params = {
      content: {},
      id: this.tagData['id'],
      sessionId: localStorage.getItem('sessionId')
    };
    this.pageSize = this.tagData['pageSize'];
    this.pageTotal = this.tagData['pageTotal'];

    if (!this.subscription) {
      // 获取筛选参数
      this.subscription = this.message.getMessage().subscribe(param => {
        // 重置当前页数
        this.pageNo = 1;
        // 参数
        this.params['content'] = param;
        // 页面数据参数
        this.params['content']['pageNo'] = this.pageNo;
        // 请求获取新数据
        this.http.post('application/submit', this.params, res => {
          // 请求数据成功
          if (res['success']) {
            // 筛选数据
            this.tagData['data'] = res['data']['list'];
          } else {
            // 请求数据失败
            this.appGlobalService.showToast(res['message'], 2000, 'middle');
          }
        });
      });
    }
  }

  ngOnDestroy(): void {
    // 组件生命周期结束的时候，注销
    this.subscription.unsubscribe();
  }

  /**
   * 列表跳转
   *
   * @param {*} target 页面id
   * @param {*} id 列表项id
   * @memberof BasicListComponent
   */
  clickLink(target, _id) {
    const pageParams = {
      pageId: target,
      params: { id: _id },
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }

  /**
   * 电话
   *
   * @param {*} phone 号码
   * @memberof BasicListComponent
   */
  call(phone) {
    console.log(phone);
    this.http.changeCallNumber(phone);
  }

  /**
   * 下拉刷新
   * 暂时保留：没有使用
   * @param {*} event
   * @memberof BasicListComponent
   */
  doRefresh(event) {
    // TODO: 下拉刷新
  }

  /**
   * 上滑加载
   *
   * @param {*} event
   * @memberof BasicListComponent
   */
  loadData(event) {
    // 请求下一个页面
    this.pageNo = this.pageNo + 1;
    // 参数
    this.params['content']['pageNo'] = this.pageNo;
    // 请求获取新数据
    this.http.post('application/submit', this.params, res => {
      // 请求数据成功
      if (res['success']) {
        // 追加返回数据
        if (Array.isArray(res['data']['list']) && res['data']['list'].length > 0) {
          // 追加数据
          res['data']['list'].map(item => {
            this.tagData['data'].push(item);
          });
        } else {
          // 没有数据了
          // this.appGlobalService.showToast('没有数据了...', 1000, 'middle');
        }
        event.target.complete();
      } else {
        // 请求失败，更新当前页数
        this.pageNo = this.pageNo - 1;
        // 请求数据失败
        this.appGlobalService.showToast(res['message'], 2000, 'middle');
        event.target.complete();
      }
    });
  }
}
